/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Login;

import airtrafficcontrol.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.security.MessageDigest;
import java.util.Scanner;

/**
 *
 * @author bambo_000
 */
public class UserManagement {
//    public boolean addNewUser(String userInfo, String filename) {       
//        try 
//        {    
//            BufferedWriter bw = new BufferedWriter(new FileWriter(filename,true));
//            bw.write(userInfo);  
//            bw.newLine();
//            bw.flush();            
//            bw.close();     
//        }
//        catch(Exception e){            }     
//
//	return true;
//    }
    
    public String[] findItem(String itemname, String filename) 
    {
        try
        {
            Scanner afile = new Scanner(new File(filename));
            String data;
            
            while(afile.hasNextLine())
            {
                data = afile.nextLine();
                String[] cusInfo = data.split(":");
                
                if ( cusInfo[0].equals(itemname) )
                {
                    afile.close();
                    return cusInfo;
                }
            }
            afile.close();  
        }
        catch ( Exception e )
        {
            System.out.println( "Customer info txt file is not found!\n");
            return null;
        }
        
        return null;
    }
        
    public String validateUser(String salt, String password) {
        //String[] salt = findItem(username,"salt.txt");
        //String[] shadow = findItem(username,"shadow.txt");
      
        // Testing Code
//        System.out.println(username + " found in salt.txt\n"
//                    + "salt retrieved:" +  salt[1]
//                    + "\nhashing ...");
//        System.out.println("hash value: " + shadow[1]);
//        System.out.println("Clearance: " + shadow[2]);
        
        
//        if (shadow[1].equals(MD5(password+salt[1])))
//		return shadow[2];

	System.out.println("Password is Wrong!Please try again!");
        return null;
    }
    
    
    
    public static final String MD5(String s) {
        char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};       

        try {
            byte[] btInput = s.getBytes();
            MessageDigest mdInst = MessageDigest.getInstance("MD5");

            mdInst.update(btInput);

            byte[] md = mdInst.digest();

            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
